<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/11/8
 * Time: 13:36
 */

namespace app\index\controller;
use app\index\base_controller\BaseController;
use app\index\table\CarTable;
use think\Db;
use think\Exception;

class Car extends BaseController{

    /**
     * page--基本信息
     */
    public function carList(){
        return view('car_list');
    }

    /**
     * action--获取信息
     */
    public function getCarList(){
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'FACILITY_NO desc';
        }

        $condition = null;
        $listCondition = Login::getListConditionCar();
        if (!empty($listCondition['FACILITY_NO'])) {
            $value = $listCondition['FACILITY_NO'];
            $condition[] = ['FACILITY_NO','like',"$value%"];
        }
        if (!empty($listCondition['FACILITY'])) {
            $value = $listCondition['FACILITY'];
            $condition[] = ['FACILITY','like',"%$value%"];
        }
        if (!empty($listCondition['LICENSE_NUMBER'])) {
            $value = $listCondition['LICENSE_NUMBER'];
            $condition[] = ['LICENSE_NUMBER','like',"%$value%"];
        }

        if (!empty($listCondition['EXHAUST_VOLUME_START'])) {
            $value = $listCondition['EXHAUST_VOLUME_START'];
            $condition[] = ['EXHAUST_VOLUME','>=',$value];
        }
        if (!empty($listCondition['EXHAUST_VOLUME_END'])) {
            $value = $listCondition['EXHAUST_VOLUME_END'];
            $condition[] = ['EXHAUST_VOLUME','<=',$value];
        }

        if (!empty($listCondition['ENTERTIME_START'])) {
            $value = Db::raw("to_date('{$listCondition['ENTERTIME_START']}','yyyy-mm-dd')");
            $condition[] = ['ENTERTIME','>=',$value];
        }
        if (!empty($listCondition['ENTERTIME_END'])) {
            $value = Db::raw("to_date('{$listCondition['ENTERTIME_END']}','yyyy-mm-dd')");
            $condition[] = ['ENTERTIME','<=',$value];
        }

        try{
            $listSql = CarTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = CarTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);

            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page--新增
     */
    public function addPage(){
        $config = CarTable::getConfigAdd();
        $data = null;
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('car_add');
    }

    /**
     * action 新增
     */
    public function add() {
        $data = input();
        try{
            CarTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page--编辑
     */
    public function editPage(){
        $carNo = input('FACILITY_NO');
        $config = CarTable::getConfigEdit();
        $data = CarTable::get($carNo);
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('car_edit');
    }

    /**
     * 编辑
     */
    public function edit() {
        $carNo = input('CAR_NO');
        $data = input();
        try{
            CarTable::update(['CAR_NO'=>$carNo],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * action 批量删除
     */
    public function deleteAll(){
        $list = input('list');
        Db::startTrans();
        try{
            foreach ($list as $rateInId){
                CarTable::delete(['CAR_NO'=>$rateInId]);
            }
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page--高级查询
     */
    public function searchPage(){
        $this->view->engine->layout('layout_popup');
        return view('car_search');
    }

    /**
     * page 导出Excel
     */
    public function exportExcel(){
        $search = input('search');
        $sortStr = 'FACILITY_NO desc';

        $condition = null;
        if (!empty($search)) {
            $condition[] = ['FACILITY_NO|FACILITY','like',"%$search%"];
        }
        try{
            $list = CarTable::table()->where($condition)->order($sortStr)->select();
            CarTable::exportExcel($list,'车辆');
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }


    /**
     * 打印
     */
    public function printPage() {
        $id = input('id');
        $data = CarTable::get($id);
        if(empty($data)){
            return '查询车辆信息失败';
        }
        $config = CarTable::getConfigPrint();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('car_print');
    }


    public function addConditionSession() {
        $condition = input();
        Login::setListConditionCar($condition);
        return json_encode(['code'=>'200']);
    }

    public function importPage() {
        $this->view->engine->layout('layout_popup');
        return view('import');
    }

    public function import() {
        $inputData = input('data');
        $postData = base64_decode($inputData);
        $list = json_decode($postData,true);
        $start = input('start');

        Db::startTrans();
        $fieldList = CarTable::CAR_FIELD_LIST;
        $fieldCount = count($fieldList);
        foreach ($list as $key => $row) {
            $data = [];

            // 字段赋值
            $i = 0;
            foreach ($row as $key1=>$value) {
                $data[$fieldList[$i]] = $value;
                $i++;
                if ($i>=$fieldCount) {
                    break;
                }
            }

            try{
                CarTable::addImport($data);
            }
            catch (Exception $e){
                Db::rollback();
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错",'data'=>$e->getMessage()]);
            }
        }

        Db::commit();
        return json_encode(['code'=>'200','msg'=>'导入成功']);
    }

}